package com.hb.guli.service.edu.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hb.guli.common.base.result.R;
import com.hb.guli.service.edu.entity.Teacher;
import com.hb.guli.service.edu.entity.vo.TeacherQueryVo;
import com.hb.guli.service.edu.feign.OssFileService;
import com.hb.guli.service.edu.service.TeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author jack
 * @since 2023-06-30
 */
@CrossOrigin
@Api("讲师管理")
@RestController
@RequestMapping("/admin/edu/teacher")
@Slf4j
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private OssFileService ossFileService;

    @ApiOperation("所有讲师列表")
    @GetMapping("list")
    public R listAll() {
        List<Teacher> list = teacherService.list();
        return R.ok().data("items",list);
    }

    @ApiOperation(value = "跟据id删除讲师", notes = "跟据id对讲师进行逻辑删除")
    @DeleteMapping("remove/{id}")
    public R removeById(@ApiParam("讲师id") @PathVariable String id) {
        // 删除讲师头像
        teacherService.deleteAvatarById(id);
        // 删除讲师
        boolean result = teacherService.removeById(id);
        if(result){
            return R.ok().message("删除成功");
        }
        return R.error().message("删除失败");
    }

    @ApiOperation(value = "讲师分页列表")
    @GetMapping("list/{page}/{limit}")
    public R listPage(@ApiParam(value = "当前页码", required = true) @PathVariable Long page,
                      @ApiParam(value = "每页页数",required = true) @PathVariable Long limit,
                      @ApiParam("讲师列表查询对象") TeacherQueryVo teacherQueryVo) {
        Page<Teacher> pageParam = new Page<>(page, limit);
        Page<Teacher> pageModel = teacherService.selectPage(pageParam,teacherQueryVo);
        List<Teacher> records = pageModel.getRecords();
        long total = pageModel.getTotal();
        return R.ok().data("total",total).data("rows",records);
    }

    @ApiOperation(value = "新增讲师")
    @PostMapping("save")
    public R save(@ApiParam("讲师对象") @RequestBody Teacher teacher) {
        boolean save = teacherService.save(teacher);
        if(save){
            return R.ok().message("新增成功");
        }
        return R.error().message("新增失败");
    }

    @ApiOperation(value = "更新讲师")
    @PutMapping("update")
    public R updateById(@ApiParam("讲师对象") @RequestBody Teacher teacher) {
        boolean updated = teacherService.updateById(teacher);
        if(updated){
            return R.ok().message("更新成功");
        }
        return R.error().message("更新失败");
    }

    @ApiOperation(value = "更新讲师")
    @GetMapping("get/{id}")
    public R getById(@ApiParam(value = "当前页码", required = true) @PathVariable String id) {
        Teacher teacher = teacherService.getById(id);
        if(teacher != null){
            return R.ok().data("item",teacher);
        }
        return R.error().message("数据不存在");
    }


    @ApiOperation(value = "批量删除讲师")
    @DeleteMapping("batch-remove")
    public R batchRemove(@ApiParam("讲师id列表") @RequestBody List<String> idList) {
        boolean result = teacherService.removeBatchByIds(idList);
        if(result){
            return R.ok().message("删除成功");
        }
        return R.error().message("删除失败");
    }


    @ApiOperation("跟据关键字查询所有讲师姓名列表")
    @GetMapping("list/name/{key}")
    public R listAllName(@PathVariable String key) {
        List<Map<String,Object>> list = teacherService.selectNameList(key);
        return R.ok().data("nameList",list);
    }

    @ApiOperation("测试")
    @GetMapping("test")
    public R test(){
        ossFileService.test();
        return R.ok();
    }

    @ApiOperation("测试并发")
    @GetMapping("test_concurrent")
    public R test_concurrent(){
        log.info("test_concurrent");
        return R.ok();
    }

    @GetMapping("message1")
    public String message1(){
        return "message1";
    }

    @GetMapping("message2")
    public String message2(){
        return "message2";
    }
}
